package com.everflourish.yeah100.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.everflourish.yeah100.R;
import com.everflourish.yeah100.ui.XListView;
import com.everflourish.yeah100.utils.CommonUtil;

import java.util.List;

/**
 * 单选对话框对话框
 *
 * @author p大田
 */
public class BluetoothDeviceDialog extends Dialog implements DialogInterface {

    public static final int BUTTON_CONFIRM = -1;

    public static final int BUTTON_CANCEL = -2;

    private Context mContext;
    private TextView mTitleTv;
    private XListView mSingleLv;
    private String mTitle;
    private List<String> mItems;
    private TextView mHelpTv;

    private OnClickListener onClickListener;
    private View.OnClickListener onHelpClickListener;
    private XListView.IXListViewListener onXListViewListener;

    public BluetoothDeviceDialog(Context context, String title) {
        super(context, R.style.MyDialog);
        this.mContext = context;
        setContentView(R.layout.dialog_bluetooth);
        mTitleTv = (TextView) this.findViewById(R.id.title_tv);
        mHelpTv = (TextView) this.findViewById(R.id.help_tv);
        mSingleLv = (XListView) this.findViewById(R.id.single_lv);
        mSingleLv.setPullLoadEnable(true);
        mSingleLv.setPullMore(false);
        this.mTitle = title;
    }

    public BluetoothDeviceDialog(Context context) {
        this(context, null);
    }

    public BluetoothDeviceDialog setXListViewListener(
            XListView.IXListViewListener listener) {
        onXListViewListener = listener;
        return this;
    }

    public BluetoothDeviceDialog stopLoadMore() {
        if (onXListViewListener != null) {
            mSingleLv.stopLoadMore();
        }
        return this;
    }

    public void setHelp(View.OnClickListener onClickListener) {
        this.onHelpClickListener = onClickListener;
    }

    public BluetoothDeviceDialog stopRefresh() {
        if (onXListViewListener != null) {
            mSingleLv.stopRefresh();
        }
        return this;
    }

    public BluetoothDeviceDialog notifyDataSetChanged() {
        if (singleAdapter != null) {
            singleAdapter.notifyDataSetChanged();
        }
        return this;
    }

    /**
     * 设置标题
     *
     * @param title
     * @return PromptDialog
     */
    public BluetoothDeviceDialog setTitle(String title) {
        this.mTitle = title;
        return this;
    }

    public BluetoothDeviceDialog setItems(List<String> items,
                                          OnClickListener listener) {
        this.mItems = items;
        this.onClickListener = listener;
        return this;
    }

    @Override
    public void show() {
        createDialog();
        super.show();
    }

    private BluetoothDeviceDialog createDialog() {

        if (mTitle == null) {
            mTitleTv.setVisibility(View.GONE);
        } else {
            mTitleTv.setText(mTitle);
        }
        if (mItems == null) {
            mSingleLv.setVisibility(View.GONE);
        } else {
            mSingleLv.setAdapter(singleAdapter);
        }

        if (onClickListener != null) {
            mSingleLv
                    .setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent,
                                                View view, int position, long id) {
                            onClickListener.onClick(BluetoothDeviceDialog.this,
                                    position);
                            BluetoothDeviceDialog.this.dismiss();
                        }
                    });
        }
        if (onHelpClickListener != null) {
            mHelpTv.setVisibility(View.VISIBLE);
            mHelpTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onHelpClickListener.onClick(v);
                }
            });
        }
        if (onXListViewListener != null) {
            mSingleLv.setXListViewListener(onXListViewListener);
        }
        return this;
    }

    public ListView getListView() {
        return mSingleLv;
    }

    BaseAdapter singleAdapter = new BaseAdapter() {

        @Override
        public int getCount() {
            return mItems.size();
        }

        @Override
        public Object getItem(int position) {
            return mItems.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            HolderView holderView = null;
            if (convertView == null) {
                holderView = new HolderView();
                convertView = LayoutInflater.from(mContext).inflate(
                        R.layout.item_dialog_bluetooth, parent, false);
                holderView.name = (TextView) convertView
                        .findViewById(R.id.dialog_bluetooth_name_tv);
                convertView.setTag(holderView);
            } else {
                holderView = (HolderView) convertView.getTag();
            }
            String bluetoothName = mItems.get(position);
            holderView.name.setCompoundDrawables(null,
                    CommonUtil.getBluetoothDrawable(mContext, bluetoothName), null, null);
            holderView.name.setText(bluetoothName);
            return convertView;
        }

        class HolderView {
            public TextView name = null;
        }
    };
}
